import router from './router'
import store from './store'
import {
  top_list,
  manage_list
} from '@/utils/data.js'
import {
  LgetItem
} from '@/utils/storage'

//定义活动管理白名单
const manage = ['/manage/setup', '/manage/comment', '/manage/statistical', '/manage/rankList']

// 白名单
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  const jwtToken = LgetItem('jwtToken')
  if (jwtToken) {
    if (manage.indexOf(to.path) != -1) {
      store.commit('menu/SET_MANAGELIST', [])
      if (!store.getters.top_list.length) {
        //像vuex中注入顶部数据
        store.commit('menu/SET_TOPLIST', top_list)
      }
    } else if (to.path === '/manage/list') {
      store.commit('menu/SET_TOPLIST', [])
      if (!store.getters.manage_list.length) {
        //像vuex中注入顶部数据
        store.commit('menu/SET_MANAGELIST', manage_list)
      }

    } else {
      store.commit('menu/SET_TOPLIST', [])
      store.commit('menu/SET_MANAGELIST', [])
    }
    next()
  } else {
    // 在免登录白名单中的path，可以直接进入，不需要验证token是否存在
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      console.log(to)
      // 只要to也就是要访问的地址不在白名单当中，我们就让他重新跳转到登录页，并且收集此时地址的path
      next({
        path: `/login?redirect=${to.path}&query=${JSON.stringify(to.query)}`
      })
    }
  }




})

router.afterEach((to) => {
    document.title = to.query.setTitle || to.meta.title
})